package ldkxingzhe.ldkxingzhedemo.view.dialogs;

import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Stack;

import ldkxingzhe.ldkxingzhedemo.R;
import ldkxingzhe.ldkxingzhedemo.utils.LogDemo;

/**
 * Created by xingzhe on 15/11/11.
 */
public class Choice2LevelDialog extends DialogFragment implements AdapterView.OnItemClickListener, View.OnClickListener {
    private static final String TAG = Choice2LevelDialog.class.getSimpleName();

    // MARK: Global variable
    private List<String> currentLevelList;
    private ArrayAdapter adapter;
    private JsonObject currentJsonObject;

    private Stack<JsonObject> mStack = new Stack<>();
    private Stack<String> mTitleStack = new Stack<>();
    private static final String JSON_STRING = "json_string";

    private boolean isJsonObject = true;

    // MARK: UI Elements
    private TextView mTitle;
    private String mTitleStr;
    /**
     * 静态构造函数
     * @param json json串
     * @return
     * */
    public static Choice2LevelDialog newInstance(String json){
        if(json == null){
            throw new IllegalArgumentException("json string is null");
        }
        Choice2LevelDialog dialog = new Choice2LevelDialog();
        Bundle args = new Bundle();
        args.putString(JSON_STRING, json);
        dialog.setArguments(args);
        return dialog;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle args = getArguments();
        String json = args.getString(JSON_STRING);
        currentJsonObject = new JsonParser().parse(json).getAsJsonObject();
        currentLevelList = new ArrayList<>();
        setCurrentList(currentJsonObject);
    }

    private void setCurrentList(JsonObject jsonObject) {
        currentLevelList.clear();
        for(Map.Entry<String, JsonElement> entry : jsonObject.entrySet()){
            currentLevelList.add(entry.getKey());
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog =  super.onCreateDialog(savedInstanceState);
        LayoutInflater layoutInflater = getActivity().getLayoutInflater();
        LinearLayout layout = (LinearLayout)layoutInflater.inflate(R.layout.choice_2_dialog,null);
        layout.findViewById(R.id.image_back).setOnClickListener(this);
        mTitleStr = "请选择";
        mTitle = (TextView)layout.findViewById(R.id.title);
        mTitle.setText(mTitleStr);

        ListView listView = (ListView)layout.findViewById(R.id.list_view);
        listView.setOnItemClickListener(this);
        adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, currentLevelList);
        listView.setAdapter(adapter);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(layout);
        return dialog;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        LogDemo.v(TAG, "onItemClick, and position is " + position);
        String choiceTitle = currentLevelList.get(position);
        if(isJsonObject){
            currentLevelList.clear();
            JsonElement jsonElement = currentJsonObject.get(choiceTitle);
            if(jsonElement.isJsonArray()){
                isJsonObject = false;
                JsonArray jsonArray = jsonElement.getAsJsonArray();
                for(JsonElement tmp : jsonArray){
                    currentLevelList.add(tmp.getAsString());
                }
            }else if(jsonElement.isJsonObject()){
                setCurrentList(jsonElement.getAsJsonObject());
            }else{
                // 这里需要注意一下, 此处写的是choiceTitle, 用的是key, 不是value. 使用的时候需要注意
                Log.v(TAG,"result is " + choiceTitle);
                this.dismiss();
                return;
            }
            mStack.push(currentJsonObject);
            mTitleStack.push(mTitleStr);
            mTitleStr = choiceTitle;
            mTitle.setText(choiceTitle);
            adapter.notifyDataSetChanged();
        }else{
            // choiceTitle就是结果
            Log.d(TAG,"the result is " + choiceTitle);
            this.dismiss();
            return;
        }
    }

    @Override
    public void onClick(View v) {
        if(mStack.isEmpty()){
            Toast.makeText(getActivity(), "已经在最上一级", Toast.LENGTH_SHORT).show();
            return;
        }

        isJsonObject = true;
        setCurrentList(mStack.pop());
        mTitleStr = mTitleStack.pop();
        mTitle.setText(mTitleStr);
        adapter.notifyDataSetChanged();
    }
}
